package com.gitee.dgwcode.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements ApplicationAware,ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5445766970313021353L;
	
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	private HttpServletRequest request;
	
	private Map<String, Object> application;
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application=application;
	}
	
	
	public String login() {
		Integer count = (Integer) application.get("count");
		
		if(count==null) {
			count=0;
		}
		count++;
		application.put("count", count);
		
		request.getSession().setAttribute("username", getUsername());
		
		return "onlineinfo";
	}
	
	public String logout() {
		Integer object = (Integer) this.application.get("count");
		if(object!=null&&object>0) {
			object--;
		}
		application.put("count", object);
		request.getSession().invalidate();
		return "logout";
	}

}
